<?php
class File {

    public function directoryToArray($directory,$recursive=false){
        $array_items = array();
        $i = 0;

        if(!is_dir($directory)) return "$directory folder does not exist";

        if($handle = opendir($directory)) {
            while (false !==($file = readdir($handle))) {
                if ($file != "." && $file != "..") {
                    if (is_dir($directory."/".$file)) {
                        if($recursive) {
                            $array_items = array_merge($array_items,$this->directoryToArray($directory."/".$file,$recursive));
                        }
                        //$file = $directory."/".$file;
                        //$array_items[] = preg_replace("/\/\//si", "/", $file);

                    } else{
                        $file = $directory."/".$file;
                        $ext = pathinfo($file)['extension'];
//                        print_r(pathinfo($file));
//
//                        $newFileName = pathinfo($file)['filename'];
//                        if(pathinfo($file)['extension'] == "txt"){
//                           // filename
//                            rename();
//
//                        }

                        $array_items[$i][$ext] = preg_replace("/\/\//si", "/", $file);

                    }

                }

            }
            closedir($handle);
            $i++;
        }

        return $array_items;

    }

}
